package recharge

import (
	"context"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type RechargeVipModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewRechargeVipModel(ctx context.Context, db *gorm.DB) *RechargeVipModel {
	return &RechargeVipModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *RechargeVipModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *RechargeVipModel) GetAllRechargeVip(in *rpc.RechargeVipLevelListRequest) ([]schema.RechargeVip, error) {
	var rechargeVip []schema.RechargeVip
	tx := model.getDb().Model(&schema.RechargeVip{})
	if in.Viplevel != nil {
		tx.Where("level = ?", in.Viplevel)
	}
	res := tx.Find(&rechargeVip)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeVipModel-GetAllRechargeVip,err", err)
		return nil, err
	}
	return rechargeVip, nil
}

func (model *RechargeVipModel) InsertRechargeVip(data *schema.RechargeVip) error {
	res := model.getDb().Model(&schema.RechargeVip{}).Create(data)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeVipModel-InsertRechargeVip, err", err)
		return err
	}
	return nil
}

// InsertRechargeVipBatch 批量插入
func (model *RechargeVipModel) InsertRechargeVipBatch(data *[]schema.RechargeVip) error {
	res := model.getDb().Model(&schema.RechargeVip{}).Create(data)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeVipModel-InsertRechargeVipBatch, err", err)
		return err
	}
	return nil
}

// UpdateRechargeVipBatch 批量更改
func (model *RechargeVipModel) UpdateRechargeVipBatch(data []schema.RechargeVip) error {
	res := model.getDb().Save(data)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeVipModel-UpdateRechargeVipBatch,err", err)
		return err
	}
	return nil
}

func (model *RechargeVipModel) DeleteRechargeVip(id int64) error {
	res := model.getDb().Where("id = ?", id).Delete(&schema.RechargeVip{})
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "RechargeVipModel-DeleteRechargeVip, err", err)
		return err
	}
	return nil
}
